<?php
/**
 * Object Relational Mapping class for table `room_employee_map`.
 * 
 * @package clearhealth.local.ordo
 */
class RoomEmployeeMap extends ORDataObject {
	// Fields of table.
	var $room_employee_map_id = '';
	var $person_id = '';
	var $room_id = '';

	/**
	 * Name of DB table.
	 */
	var $_table = 'room_employee_map';
	/**
	 * Internal name.
	 */ 
    var $_internalName = 'RoomEmployeeMap';
    /**
     * Primary key.
     */
    var $_key = 'room_employee_map_id';
	
    /**
     * Constructor.
     *
     * @return RoomEmployeedMap
     */
	function RoomEmployeeMap() {
		parent::ORDataObject();
	}
	
	/**
	 * Add rooms for the specific employee.
	 *
	 * @param $roomArr The array of room id
	 * @param $personId The id of employee
	 */
    public function addRoomsForEmployee($roomArr, $personId) {
        foreach ($roomArr as $roomId) {
            $roomEmp = Celini::newORDO('RoomEmployeeMap');
            $roomEmp->populateArray(array(
                            'person_id'    => $personId,
                            'room_id'   => $roomId));
            $roomEmp->persist();
        }
    }
    
    /**
     * Update rooms for the specific employee.<br/>
     * 1. Delete the old rooms of employee.<br/>
     * 2. Add new rooms for employee.<br/>
     *
     * @param $roomArr The array of room id be added
     * @param $personId The id of employee
     */
    public function updateRoomsForEmployee($roomArr, $personId) {
    	//1. Delete the old rooms of employee
        $sql = "delete from $this->_table where person_id = $personId";
        $this->_execute($sql);
        //2. Add new rooms for employee.<br/>
        $this->addRoomsForEmployee($roomArr, $personId);
    }
    	/**
	 * @author hdviet
	 * update for localize demography
	 */
    /**
     * Get mapped rooms of the specific employee.
     *
     * @param $personId The id of employee
     * @return The array of mapped rooms
     */
    public function getRoomsOfEmployee($personId) {
    	if (empty($personId)) {
    		return array();
    	}
    	$field	=	L10nHelper::changeField(L10nHelper::getLocale(), 'name');		
	
    	$ds = new Datasource_sql();
        $sql = array (
            'cols'      => "r.room_id, r.$field as name",
            'from'      => "$this->_table re INNER JOIN room r ON re.room_id = r.room_id ",
            'where'     => "re.person_id = $personId",
            'orderby'   => "r.name"
            );      
        $ds->setup($this->_db, $sql);
        return $ds->toArray('room_id', 'name');
    }
}
?>